﻿using LoveEat.Helper;
using System;
using System.Linq;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using LoveEat.MODEL;
using System.Linq.Expressions;
using Common.Logging;
using System.Web;
using Common;
namespace LoveEat.AdminController
{
    public class MenuManagementController : BaseController
    {
        private readonly ILog _fileLog = LogManager.GetLogger(typeof(MenuManagementController));
        #region 菜系管理
        /// <summary>
        /// 管理
        /// </summary>
        /// <returns></returns>
        public ActionResult MenuCategories(LoveEat.MODEL.PageFilter filter, string menu_series_name)
        {
            var where = PredicateBuilder.True<sys_menu_series>();
            //Expression<Func<sys_menu_series, bool>> exp = a => true;
            if (!string.IsNullOrEmpty(menu_series_name))
            {

                where = where.And(p => p.menu_series_name == menu_series_name);
            }
            var menuSeriesPageList = OperateContext.Current.BLLSession.Isys_menu_seriesBLL.
                              GetPagedList(filter.PageIndex, filter.PageSize,
                              ref RowCount,
                            where, s => s.menu_series_id);
            Webdiyer.WebControls.Mvc.PagedList<LoveEat.MODEL.sys_menu_series> pageList = menuSeriesPageList.ToPagedList(filter.PageIndex, filter.PageSize);
            pageList.TotalItemCount = RowCount;
            pageList.CurrentPageIndex = filter.PageIndex;
            return View(pageList);
        }
        [HttpGet]
        public ActionResult MenuCategoriesEdit(sys_menu_series model)
        {
            ViewBag.sys_menuTypeSelLists = new SelectList(OperateContext.Current.BLLSession.Isys_menu_typeBLL.GetListBy(s => true).ToList(), "menu_type_id", "menu_type_name").AsEnumerable();

            if (model.menu_series_id > 0)
            {
                model = OperateContext.Current.BLLSession.Isys_menu_seriesBLL.GetListBy(s => s.menu_series_id == model.menu_series_id).FirstOrDefault();
            }
            return View(model);
        }

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult MenuCategoriesEdit(MODEL.sys_menu_series model, string returnUrl)
        {
            var result = 0;
            if (ModelState.IsValid)
            {
                model.updateTime = DateTime.Now;
                if (model.menu_series_id > 0) //update
                {

                    //result = OperateContext.Current.BLLSession.Isys_menu_seriesBLL.Modify(model,
                    //    new[] { "menu_type_id", "menu_series_name" });

                    if (OperateContext.Current.BLLSession.Isys_menu_seriesBLL.Update(model))
                    {
                        result = 1;
                    }

                }
                else //add
                {

                    result = OperateContext.Current.BLLSession.Isys_menu_seriesBLL.Add(model);
                }
                if (result > 0)
                {

                    return Content(AlterMessage("操作成功", returnUrl));

                }
            }
            return Content(AlterMessage("操作失败", returnUrl));
        }

        public ActionResult DeleteMenuCategories(MODEL.sys_menu_series model, string returnUrl)
        {

            if (OperateContext.Current.BLLSession.Ishop_menuBLL.GetListBy(s
                   => s.series_id == model.menu_series_id).FirstOrDefault() != null)
            {
                return Content(AlterMessage("该系列下有商品无法删除", returnUrl));
            }
            int result = OperateContext.Current.BLLSession.Isys_menu_seriesBLL.Del(model);

            if (result > 0)
            {

                return Content(AlterMessage("删除成功", returnUrl));
            }
            return Content(AlterMessage("删除失败", returnUrl));
        }
        public ActionResult DeleteMenuCategoriesList(string id)
        {
            int[] ids = id.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
            if (OperateContext.Current.BLLSession.Ishop_menuBLL.GetListBy(s
                            => ids.Contains(s.series_id)).FirstOrDefault() != null)
            {
                return Json(0);
            }

            int result = OperateContext.Current.BLLSession.Isys_menu_seriesBLL.DelBy(s
                => ids.Contains(s.menu_series_id));
            return Json(result);
        }
        #endregion

        #region 菜单管理
        public ActionResult SysMenuType(LoveEat.MODEL.PageFilter filter)
        {
            var where = PredicateBuilder.True<sys_menu_type>();

            var ss = OperateContext.Current.BLLSession.Isys_menu_typeBLL.
                              GetPagedList(filter.PageIndex, filter.PageSize,
                              ref RowCount,
                            where, s => s.menu_type_id);
            Webdiyer.WebControls.Mvc.PagedList<LoveEat.MODEL.sys_menu_type> pageList = ss.ToPagedList(filter.PageIndex, filter.PageSize);
            pageList.TotalItemCount = RowCount;
            pageList.CurrentPageIndex = filter.PageIndex;
            return View(pageList);
        }
        [HttpGet]
        public ActionResult SysMenuTypeEdit(sys_menu_type model)
        {

            if (model.menu_type_id > 0)
            {
                model = OperateContext.Current.BLLSession.Isys_menu_typeBLL.GetListBy(S
                       => S.menu_type_id == model.menu_type_id).FirstOrDefault();
            }
            return View(model);
        }
        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult SysMenuTypeEdit(sys_menu_type model, string returnUrl)
        {
            var result = 0;
            if (ModelState.IsValid)
            {
                model.updateTime = DateTime.Now;
                if (model.menu_type_id > 0) //update
                {
                    if (OperateContext.Current.BLLSession.Isys_menu_typeBLL.Update(model))
                    {
                        result = 1;

                    }
                }
                else //add
                {
                    result = OperateContext.Current.BLLSession.Isys_menu_typeBLL.Add(model);
                }
                if (result > 0)
                {

                    return Content(AlterMessage("操作成功", returnUrl));

                }
            }
            return Content(AlterMessage("操作失败", returnUrl));
        }
        public ActionResult DeleteSysMenuType(sys_menu_type model, string returnUrl)
        {
            if (OperateContext.Current.BLLSession.Isys_menu_seriesBLL.GetListBy(s
                => s.menu_type_id == model.menu_type_id).FirstOrDefault() != null)
            {
                return Content(AlterMessage("该分类下有菜系无法删除", returnUrl));
            }
            int result = OperateContext.Current.BLLSession.Isys_menu_typeBLL.Del(model);

            if (result > 0)
            {
                return Content(AlterMessage("删除成功", returnUrl));
            }
            return Content(AlterMessage("删除失败", returnUrl));
        }
        public ActionResult DelteSysMenuTypeList(string id)
        {
            int[] ids = id.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

            if (OperateContext.Current.BLLSession.Isys_menu_seriesBLL.GetListBy(s
                          => ids.Contains(s.menu_type_id)).FirstOrDefault() != null)
            {
                return Json(0);
            }
            int result = OperateContext.Current.BLLSession.Isys_menu_typeBLL.DelBy(s
                          => ids.Contains(s.menu_type_id));
            return Json(result);
        }
        #endregion

        #region 菜品管理

        public ActionResult ShopMenu(PageFilter filter, string menu_name)
        {
            var where = PredicateBuilder.True<shop_menu>();
            Expression<Func<shop, bool>> exp = a => true;
            if (!string.IsNullOrEmpty(menu_name))
            {
                where = where.And(p => p.menu_name.Contains(menu_name));
            }
            where = where.And(p => p.status == 1);
            var shopMenuPageList = OperateContext.Current.BLLSession.Ishop_menuBLL.
                              GetPagedList(filter.PageIndex, filter.PageSize,
                              ref RowCount,
                            where, s => s.shop_id);

            foreach (var item in shopMenuPageList)
            {
                var shop = OperateContext.Current.BLLSession.IshopBLL.GetListBy(p => p.shop_id == item.shop_id).FirstOrDefault();
                var sysMenuSeries = OperateContext.Current.BLLSession.Isys_menu_seriesBLL.GetListBy(p => p.menu_series_id == item.series_id).FirstOrDefault();
                item.ShopName = shop != null ? shop.shop_name : item.ShopName;
                item.SeriesName = sysMenuSeries != null ? sysMenuSeries.menu_series_name : item.SeriesName;
            }
            Webdiyer.WebControls.Mvc.PagedList<shop_menu> pageList = shopMenuPageList.ToPagedList(filter.PageIndex, filter.PageSize);
            pageList.TotalItemCount = RowCount;
            pageList.CurrentPageIndex = filter.PageIndex;
            return View(pageList);
        }

        [HttpGet]
        public ActionResult ShopMenuEdit(shop_menu model)
        {

            if (model.menu_id > 0)
            {
                model = OperateContext.Current.BLLSession.Ishop_menuBLL.GetListBy(s => s.menu_id == model.menu_id).FirstOrDefault();

            }
            ViewBag.shopList = new SelectList(OperateContext.Current.BLLSession.IshopBLL.GetListBy(s => s.status == 1).ToList(), "shop_id", "shop_name").AsEnumerable();
            ViewBag.seriesList = new SelectList(OperateContext.Current.BLLSession.Isys_menu_seriesBLL.GetListBy(s => true).ToList(), "menu_series_id", "menu_series_name").AsEnumerable();
            return View(model);
        }

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult ShopMenuEdit(LoveEat.MODEL.shop_menu model, string returnUrl, HttpPostedFileBase imageUpLoad)
        {
            #region 图片上传
            string pImgSrc = string.Empty;
            if (imageUpLoad != null)
            {
                if (!imageUpLoad.ContentType.Contains("image/"))
                {
                    return Content(AlterMessage("请上传正确的图片", returnUrl));
                }

                //1.2验证图片大小
                if (imageUpLoad.ContentLength <= 2 || imageUpLoad.ContentLength > 4 * 1024 * 1024)
                {
                    return Content(AlterMessage("图片太小或太大（1M以内）~！", returnUrl));
                }
                //1.2 生成 新的图片名
                pImgSrc = FileHelper.NewFileName(imageUpLoad.FileName);
                //1.3 生成 缩略图变名
                //    string strNewThumbName = FileHelper.GetThumbName(strNewImgName);

                ////2.保存图片 和 生成缩略图
                FileHelper.SaveImgAndThumb(imageUpLoad, pImgSrc, "/upload/image/shop_menu/", 145, 145);
            }
            #endregion

            if (ModelState.IsValid)
            {
                model.updateTime = DateTime.Now;
                var result = 0;
                if (model.menu_id > 0) //update
                {

                    if (imageUpLoad != null)
                    {
                        model.menu_icon = "/upload/image/shop_menu/" + pImgSrc;
                    }
                    //result = OperateContext.Current.BLLSession.Ishop_menuBLL.Modify(model,
                    //                        new[] { "series_id", "price", "menu_icon", "menu_name", "shop_id" });
                    if (OperateContext.Current.BLLSession.Ishop_menuBLL.Update(model)) ;
                    result = 1;

                }
                else //add
                {
                    model.status = 1;
                    model.menu_icon = "/upload/image/shop_menu/" + pImgSrc;
                    result = OperateContext.Current.BLLSession.Ishop_menuBLL.Add(model);
                }
                if (result > 0)
                {

                    return Content(AlterMessage("操作成功", returnUrl));

                }
            }
            return Content(AlterMessage("操作失败", returnUrl));
        }
        public ActionResult DeleteShopMenu(shop_menu model, string returnUrl)
        {
            int result = OperateContext.Current.BLLSession.Ishop_menuBLL.Del(model);

            if (result > 0)
            {

                return Content(AlterMessage("删除成功", returnUrl));
            }
            return Content(AlterMessage("删除失败", returnUrl));
        }
        public ActionResult DeleteShopMenuList(string id)
        {
            int[] ids = id.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
            int result = OperateContext.Current.BLLSession.Ishop_menuBLL.DelBy(s
                           => ids.Contains(s.menu_id));
            return Json(result);
        }



        #endregion



    }


}
